<%@ page pageEncoding="utf-8"%>
<%@page import="org.myhibernate.util.Json"%>
<%@page import="org.myhibernate.Template"%>
<%@page import="org.myhibernate.ProxyTemplate"%>
<%@page import="org.myhibernate.core.method.PageSizeQuery"%>
<%@page import="java.util.List"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="org.personal.entity.Page"%>
<%@page import="org.personal.entity.Layout"%>
<%@page import="org.personal.entity.User"%>
<%@page import="org.personal.Constants"%>
<%@page import="org.personal.entity.UserPage"%>
<%@page import="org.personal.util.UserUtil"%>
<%@page import="org.personal.entity.Portlet"%>
<%!
public void toList(List list,String str,String sep)
{
	if((list==null)||(str==null)||(sep==null)){return;}
	list.clear();
	String[] ss=str.split(sep);
	for(int i=0;i<ss.length;i++)
	{
		String temp=ss[i];
		if((temp!=null)&&(temp.trim().length()>0))
		{
			list.add(temp);
		}
	}
}
%>
<%
User user=(User)request.getSession().getAttribute(Constants.USER);
Json json=new Json();
if(user!=null)
{
	String pageId=request.getParameter("id");
	Template template=new ProxyTemplate(Page.class).getInstance();
	try
	{
		if((pageId==null)||("".equals(pageId)))
		{
			pageId="-1";
		}
		Page pageObject=(Page)template.get(Integer.parseInt(pageId));
		boolean isCreate=false;
		if(pageObject ==null)
		{
			pageObject=new Page();
			isCreate=true;
		}
		//
		int layout=-1;
		String title=request.getParameter("title");
		String pagePath=request.getParameter("pagePath");
		String adminUsers=request.getParameter("adminUsers");
		String editUsers=request.getParameter("editUsers");
		String users=request.getParameter("users");
		String layoutString=request.getParameter("layout");
		String portlets=request.getParameter("portlets");
		if((layoutString!=null)&&(layoutString.trim().length()>0))
		{
			layout=Integer.parseInt(layoutString);
		}
		
		title=URLDecoder.decode(title,"utf-8");
		pageObject.setTitle(title);
		pageObject.setPagePath(pagePath);
		pageObject.setLayout(layout);
		toList(pageObject.getAdminUsers(),adminUsers,",");
		toList(pageObject.getEditUsers(),editUsers,",");
		toList(pageObject.getUsers(),users,",");
		toList(pageObject.getPortlets(),portlets,",");
		
		if(isCreate)
		{
			template.save(pageObject);
		}else
		{
			template.update(pageObject);
		}
		json.add("result","ok");
	}catch(Exception exception)
	{
		json.add("result","no");
		json.add("message",exception.toString().trim());
	}
	finally
	{
		template.close();
	}
}else
{
	json.add("result","no");
	json.add("message","用户未登录");
}
out.println(json);
%>
